<script setup>
import { ref } from "vue";
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";

/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.ProductList,
  routerPre: 'ProductList',
  params: {
    name: '',
  }
})
const categoryList = ref([])
http.Category.list().then(res => {
  categoryList.value = res.data
})
</script>


<template>
  <PageMain title="商品列表">
    <el-form label-suffix="：" inline>
<!--      商品分类下拉-->
      <el-form-item label="商品分类">
        <el-select v-model="searchParams.category_id" placeholder="请选择商品分类" style="width: 200px">
          <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="searchParams.name" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="商品分类" prop="category.name">
        </el-table-column>
        <el-table-column label="商品名称" prop="name" min-width="160px">
        </el-table-column>
        <el-table-column label="封面">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="image" :image-url="scope.row.image"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="价格" prop="price">
        </el-table-column>
        <el-table-column label="折扣价格" prop="discount_price">
        </el-table-column>
        <el-table-column label="会员价格" prop="vip_price">
        </el-table-column>
        <el-table-column label="销量" prop="sale_count">
        </el-table-column>
        <el-table-column label="收藏数量" prop="collect_count">
        </el-table-column>
        <el-table-column label="库存" prop="store_count">
        </el-table-column>
        <el-table-column label="上架状态" >
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'error'">{{ scope.row.status === 1 ? '上架' : '下架' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="新品">
          <template #default="scope">
            <el-tag :type="scope.row.is_new === 1 ? 'primary' : 'info'">{{ scope.row.is_new === 1 ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="推荐">
          <template #default="scope">
            <el-tag :type="scope.row.is_recommend === 1 ? 'primary' : 'info'">{{ scope.row.is_recommend === 1 ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" fixed="right">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
